
package com.producttypemodule.servlet;
import com.producttypemodule.bean.TbProductType;
import com.producttypemodule.dao.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbProductTypeServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        int productTypeLevel=Integer.parseInt(request.getParameter("productTypeLevel"));
        String productTypeName=request.getParameter("productTypeName");
        int parentTypeId=Integer.parseInt(request.getParameter("parentTypeId"));
        int isMaxProductType=Integer.parseInt(request.getParameter("isMaxProductType"));
        String productTypeMemo=request.getParameter("productTypeMemo");

            if(new TbProductTypeDAO().add(new TbProductType(productTypeLevel,productTypeName,parentTypeId,isMaxProductType,productTypeMemo))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
             request.getSession(true).setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
            response.sendRedirect("ProducttypeModule/TbProductTypeList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_ProductType", "ProductTypeId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
            response.sendRedirect("ProducttypeModule/TbProductTypeList.jsp");
        }
        if("edit".equals(method)){
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
        int productTypeLevel=Integer.parseInt(request.getParameter("productTypeLevel"));
        String productTypeName=request.getParameter("productTypeName");
        int parentTypeId=Integer.parseInt(request.getParameter("parentTypeId"));
        int isMaxProductType=Integer.parseInt(request.getParameter("isMaxProductType"));
        String productTypeMemo=request.getParameter("productTypeMemo");

            if(new TbProductTypeDAO().edit(new TbProductType(productTypeId,productTypeLevel,productTypeName,parentTypeId,isMaxProductType,productTypeMemo))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
            response.sendRedirect("ProducttypeModule/TbProductTypeList.jsp");
        }
        if ("list".equals(method)) {
             request.getSession(true).setAttribute("tbProductTypeList",new TbProductTypeDAO().getTbProductTypeAll());
            response.sendRedirect("ProducttypeModule/TbProductTypeList.jsp");
        	}
        if ("listById".equals(method)) {
        	System.out.println("method");
        	request.getSession(true).setAttribute("productTypeId", request.getParameter("productTypeId"));
        	request.getSession(true).setAttribute("ParentTypeList",new TbProductTypeDAO().getTbProductTypeByParentTypeId(-1));
            
        	request.getSession(true).setAttribute("tbProductType",new TbProductTypeDAO().getTbProductTypeByProductTypeId(Integer.parseInt(request.getParameter("productTypeId"))));
            response.sendRedirect("ProducttypeModule/TbProductTypeOperate.jsp");
    	}
        if("showadd".equals(method)){
        	 
        	request.getSession(true).setAttribute("ParentTypeList",new TbProductTypeDAO().getTbProductTypeByParentTypeId(-1));
        	request.getSession(true).setAttribute("productTypeId", "");
            request.getSession(true).removeAttribute("tbProductType");
            response.sendRedirect("ProducttypeModule/TbProductTypeOperate.jsp");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
